<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model
{
	function __construct() 
	{
        parent::__construct();
	} 
	
	// Begin List user
	function get_all_user($num,$offset,$arr_search)
	{
		/*Begin search*/
		switch($arr_search['field_search'])
		{
			case 'email':
				$this->db->like('lb_email',$arr_search['key_search']);
				break ;
			case 'fullname':
				$this->db->like('fullname',$arr_search['key_search']);
				break ;
			case 'nickname':
				$this->db->like('username',$arr_search['key_search']);
				break ;
			default :
				break ;
		}
		/*End search*/
		
         $this->db->where('(`bl_active`=1 OR `bl_active` = 0)');
		 $this->db->order_by('dt_login','DESC');
		 $query = $this->db->get('member',$num,$offset);
		 return $query->result();
	}
	
	function get_num_user($arr_search)
	{
		/*Begin search*/
		switch($arr_search['field_search'])
		{
			case 'email':
				$this->db->like('lb_email',$arr_search['key_search']);
				break ;
			case 'fullname':
				$this->db->like('fullname',$arr_search['key_search']);
				break ;
			case 'nickname':
				$this->db->like('username',$arr_search['key_search']);
				break ;
			default :
				break ;
		}
		/*End search*/
         $this->db->where('bl_active',1);
		 $query = $this->db->get('member');
		 return $query->num_rows();
	}
	
	
	//Save user
	function save_user($member_id,$data)
	{
		 if($member_id!=0){
			  $this->db->where('member_id',$member_id);
			  if($this->db->update('member',$data)){
					return true;
			  }else{
					return false;
			  }
		 }else{
			  if($this->db->insert('member',$data)){
					return true;
			  }else{
					return false;
			  }
		 }
	}
	
	function get_list_login_code(){
		 $this->db->order_by('lb_name','ASC');
		 return $this->db->get('sys_login_code')->result();
	}   
	
	function del_user($id)
	{
		 $this->db->select('member_id,lb_image');
		 $this->db->where('member_id',$id);
		 $check = $this->db->get('member')->row();
		 if($check){
			  if(file_exists('../uploads/images/'.$check->lb_image)){
					//unlink('../uploads/images/'.$check->lb_image);
			  }
			  $this->db->where('member_id',$id);
			  if($this->db->delete('member')){
					return true;
			  }else{
					return false;
			  }
		 }else{
			  return false;
		 }
	}
	
	//Get member
	function get_user_item($member_id)
	{
		 $this->db->where('member_id',$member_id);
		 $query = $this->db->get('member');
		 return $query->row();
	}
	
	function get_user_check($lb_email,$id=0){
		 $this->db->select('member_id');
		 $this->db->where('lb_email ', trim($lb_email));
		 $this->db->where('member_id !=', $id);
		 $query = $this->db->get('member');
		 return $query->row();
	}
	function get_parent(){
		 return $this->db->get('fm_zone')->result();
	}
	
	function export_user($from='',$to='')
	{     
         if(!empty($from)) $this->db->where('dt_create', $from);
		 if(!empty($to)) $this->db->where('dt_create', $to);
		 
		 $this->db->order_by('dt_create','DESC');
		 $query = $this->db->get('member');
		 return $query->result();
	}
}
?>
